package com.xuxk.project.library.service.impl;

import com.alibaba.nacos.client.utils.JSONUtils;
import com.xuxk.project.library.service.RedisService;
import com.xuxk.project.util.ErrorCodeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 许孝康
 * @date 2020/3/26
 * @company 橙云科技有限公司
 */
@Component
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;
    /**  默认过期时长，单位：秒 */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**  不设置过期时长 */
    public final static long NOT_EXPIRE = -1;

    /**
     * 使用集合方式存储
     * @param key
     * @param value
     * @param expire 单位：秒
     * @return
     */
    @Override
    public long sadd(String key, Object value, long expire){
        long result = 0L;
        try {
            setOperations.add(key, JSONUtils.serializeObject(value));
            if(expire != NOT_EXPIRE){
                redisTemplate.expire(key, expire, TimeUnit.SECONDS);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new ErrorCodeException(1001,"添加到redis失败！");
        }
        return result;
    }
    @Override
    public Set<Object> sget(String key){
        BoundSetOperations<String, Object> stringObjectBoundSetOperations = setOperations.getOperations().boundSetOps(key);
        Set<Object> values = stringObjectBoundSetOperations.members();
        return values ;
    }
    @Override
    public long ssize(String key){
        return setOperations.size(key);
    }

    /**
     * 随机返回一个结果集
     * @param key
     * @return
     */
    @Override
    public String randomMember(String key){
        return String.valueOf(setOperations.randomMember(key));
    }

    @Override
    public void vset(String key, Object value, long expire){
        try {
            valueOperations.set(key, JSONUtils.serializeObject(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }
    @Override
    public boolean containKey(String key){
        return redisTemplate.hasKey(key);
    }
    @Override
    public void vset(String key, Object value){
        vset(key, value, DEFAULT_EXPIRE);
    }
    @Override
    public <T> T vget(String key, Class<T> clazz) {
        String value = valueOperations.get(key);

        try {
            return value == null ? null : (T) JSONUtils.deserializeObject(value,clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null ;
    }
    @Override
    public <T> T get(String key, Class<T> clazz) {
        return vget(key, clazz);
    }
    @Override
    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if(expire != NOT_EXPIRE){
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }
    @Override
    public String vget(String key) {
        return get(key, NOT_EXPIRE);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }




}
